package com.example.suspensionlist.adapter;

import android.content.Context;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.suspensionlist.R;
import com.example.suspensionlist.entity.MultipleItem;

import java.util.List;

public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem, BaseViewHolder> {

    private List<MultipleItem> mData;

    public MultipleItemQuickAdapter(Context context, List data) {
        super(data);
        addItemType(MultipleItem.LARGE, R.layout.item_large_view);
        addItemType(MultipleItem.SMALL, R.layout.item_small_view);
        mData = data;
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultipleItem item) {
        switch (helper.getItemViewType()) {
            case MultipleItem.LARGE:    // 大item
                // 标题文字
                helper.setText(R.id.tv, item.getContent());

                // 详情图
                final View tvDetails = helper.getView(R.id.tv_details);
                tvDetails.setVisibility(item.isVisibleState() ? View.VISIBLE : View.GONE);

                // 显示开关
                final int position = helper.getPosition();
                helper.setOnClickListener(R.id.btn_switch, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (tvDetails.getVisibility() == View.GONE) {
                            tvDetails.setVisibility(View.VISIBLE);
                            setViewVisible(position, true);
                        } else {
                            tvDetails.setVisibility(View.GONE);
                            setViewVisible(position, false);
                        }

                    }
                });
                break;
            case MultipleItem.SMALL:    // 小item
                helper.setText(R.id.tv, item.getContent());
                break;
        }
    }

    private void setViewVisible(int position, boolean visible) {
        mData.get(position).setVisibleState(visible);
    }


}